﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Money;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Client.UIL.ManageMoney
{
    public partial class AddAccount : UserControl
    {
        private T_Account account;
        public Boolean Drog = false;
        public int Top = 0;
        public int Left = 0;

        public AddAccount()
        {
            InitializeComponent();
            account=new T_Account();
        }
        private void AddAccount_Load(object sender, EventArgs e)
        {
            txtName.Focus();
        }

        public void GetAccount(string id)
        {
            var moneyLogic = new MoneyLogic();
            var data = moneyLogic.Where<T_Account>(m => m.Id == id).ToList()[0];
            account.Id = id;
            txtName.Text = data.Name;
            txtBankNo.Text = data.BankNo;
            txtBankName.Text = data.BankName;

            if (data.Name == "现金") txtName.ReadOnly = true;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            account.BankName = txtBankName.Text.Trim();
            account.BankNo = txtBankNo.Text.Trim();
            account.Name = txtName.Text.Trim();
            var moneyLogic = new MoneyLogic();
            string message;
            if (account.Id == null)
            {
                if (moneyLogic.Add(account) == 0) return;
                message = "添加成功";
            }
            else
            {
                if (moneyLogic.Update(account) == 0) return;
                message = "修改成功";
            }
            //FormSysMessage.ShowSuccessMsg(message);
            MoneyList.Instance.GetAccount();
            var findForm = FindForm();
            if (findForm != null) findForm.Close();
        }

        private void pbClose_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.DialogResult = DialogResult.Cancel;
            findForm.Close();
        }

        private void PnlInfoTitle_MouseDown(object sender, MouseEventArgs e)
        {
            Drog = true;
            Top = e.Y;
            Left = e.X;
        }

        private void PnlInfoTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Drog) return;
            var findForm = FindForm();
            if (findForm == null) return;
            findForm.Top += e.Y - Top;
            findForm.Left += e.X - Left;
        }

        private void PnlInfoTitle_MouseUp(object sender, MouseEventArgs e)
        {
            Drog = false;
        }


    }
}
